
import { Component, Injector, Inject, Optional, OpaqueToken, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { ModalDirective } from 'ngx-bootstrap';
import { PromotionAddComponent } from './promotion-add/promotion-add.component';
import {
  API_BASE_URL,
  PromotionListOutDto,
  PromotionServiceProxy,
  PagedResultDtoOfPromotionListForAdminOutDto,
  PromotionListForAdminOutDto, EntityDto,
  PromotionListForAdminOutDtoPromotionStatus,
  PromotionStatus
} from '@shared/service-proxies/service-proxies';
import { AppConsts } from '@shared/AppConsts';
import { varToEnumStringVal } from '@shared/helpers/UrlHelper';

@Component({
  selector: 'app-promotion-list',
  templateUrl: 'promotion.component.html',
  styleUrls: ['promotion.component.scss'],
  animations: [appModuleAnimation()]
})
export class PromotionComponent extends AppComponentBase implements OnInit {

  @ViewChild('createPromotionModal') createPromotionModal: PromotionAddComponent;

  promotionList: PagedResultDtoOfPromotionListForAdminOutDto = new PagedResultDtoOfPromotionListForAdminOutDto();
  condition: GetPromotionsCondition = new GetPromotionsCondition();
  currentPage: number = 1;
  downloadUrl: string = AppConsts.remoteServiceBaseUrl + '/file/download/';
  constructor(
    injector: Injector,
    private _promotionService: PromotionServiceProxy,
    private route: ActivatedRoute
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.getPromotions();
  }

  getPromotions(): void {

    this.condition.maxResultCount = 10;
    this._promotionService.getPromotionsForAdminAsync(this.condition.universityName,
      this.condition.promotionStatus,
      this.condition.provinceId,
      this.condition.cityId,
      this.condition.districtId,
      this.condition.keyword,
      this.condition.sorting,
      this.condition.skipCount,
      this.condition.maxResultCount)
      .subscribe((result: PagedResultDtoOfPromotionListForAdminOutDto) => {
        this.promotionList = result;
      });
  }
  pageChanged($event: any): void {
    this.condition.skipCount = this.condition.maxResultCount * $event.page;
    this.getPromotions();
  }
  createPromotion(id?: number): void {
    this.createPromotionModal.show(id);
  }

  deletePromotion(id: number): void {
    abp.message.confirm('确定要删除该推广信息么?', '请确认!', (result: Boolean) => {
      if (result) {
        this._promotionService.deletePromotionAsync(id).subscribe(() => {
          this.getPromotions();
        });
      }
    });
  }

  getStatusText(status: any): string {
    return varToEnumStringVal(status, PromotionListForAdminOutDtoPromotionStatus);
  }

  toggleTheShelf(promotion: PromotionListForAdminOutDto): void {
    if (promotion.promotionStatus === 0) {
      abp.message.confirm('确定要下架此推广信息？', '请确认', (result: boolean) => {
        if (result) {
          this._promotionService.offTheShelfAsync(promotion.id).subscribe(() => {
            this.getPromotions();
          });
        }
      });
    } else {
      this._promotionService.onTheShelfAsync(promotion.id).subscribe(() => {
        this.getPromotions();
      });
    }
  }
}


class GetPromotionsCondition {
  universityName?: string;
  promotionStatus?: PromotionStatus;
  provinceId?: number;
  cityId?: number;
  districtId?: number;
  keyword?: string;
  sorting?: string;
  skipCount?: number;
  maxResultCount?: number;
};
